package cn.edu.swust.adressbook.service.impl;

import android.content.Context;

import cn.edu.swust.adressbook.dao.DaoMaster;
import cn.edu.swust.adressbook.dao.DaoSession;
import cn.edu.swust.adressbook.service.IBaseService;


public abstract class BaseServiceImpl<T> implements IBaseService<T> {
    public static final String DATABASE_OF_USER = "database.db";
    protected DaoSession daoSession;
    protected DaoMaster daoMaster;
    protected DaoMaster.DevOpenHelper helper;

    public BaseServiceImpl(Context context){
        helper = new DaoMaster.DevOpenHelper(context, DATABASE_OF_USER);//创建数据库
        daoMaster = new DaoMaster(helper.getWritableDatabase());
        daoSession = daoMaster.newSession();
    }

    public long add(T t){
        return daoSession.insert(t);
    }



    public void update(T t){
        daoSession.update(t);
    }

    @Override
    public void delete(T t) {
        daoSession.delete(t);
    }

    public void close() {
        daoSession.clear();
        daoSession = null;
        helper.close();
        helper = null;
    }
}
